package com.cai.servlet.common.topic;

import com.cai.dao.DetailedTopicDao;
import com.cai.dao.impl.DetailedTopicDaoImpl;
import com.cai.pojo.DetailedTopic;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author RuoLi
 */
@WebServlet(name = "ServletDetailedTopicUpdate", value = "/common/detailedTopicUpdate")
public class ServletDetailedTopicUpdate extends HttpServlet {
    DetailedTopicDao detailedTopicDao = new DetailedTopicDaoImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //获取id
        int detailedTopicId = Integer.parseInt(req.getParameter("detailedTopicId"));
        DetailedTopicDao detailedTopicDao = new DetailedTopicDaoImpl();
        String data = detailedTopicDao.getDetailedTopicData(detailedTopicId);
        if (data != null) {
            resp.getWriter().write(data);
        } else {
            resp.getWriter().write("");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        Map<String, String[]> map = req.getParameterMap();
        int status = detailedTopicDao.updateDetailedTopicData(map);
        if (status!=0){
            resp.getWriter().write(status);
        }else {
            resp.getWriter().write(0);
        }

    }

    private void setMap(Map<String, String[]> map) {
        //遍历map
        DetailedTopic detailedTopic = new DetailedTopic();
        List<DetailedTopic> list = new ArrayList<>();
        map.get("topicChapterName");
        map.get("topicFrequency");
        map.get("topicSubjectName");
        map.get("topicTitle");
        map.get("topicType");
        map.get("topicCheck4");
        map.get("topicCheck3");
        map.get("topicCheck2");
        map.get("topicCheck1");
        map.get("topicAnswer");
        map.get("topicId");
        map.get("topicDetailedId");
        map.get("topicAnswerParse");
        map.get("topicKnowledgePoint");
        System.out.println(Arrays.toString(map.get("topicGrade")).replace("[", "").replace("]", ""));
    }
}
